package com.treasure.service.collocate.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.treasure.service.BaseService;
import com.treasure.service.collocate.service.BankConfigService;
import com.treasure.service.commons.lang.util.BeanUtil;
import com.treasure.service.commons.lang.util.StringUtil;
import com.treasure.service.domain.TBankConfig;
import com.treasure.service.domain.TBankConfigExample;
import com.treasure.service.domain.TCreditCard;
import com.treasure.service.domain.TCreditCardExample;
import com.treasure.service.dto.TBankConfigDto;
import com.treasure.service.dto.TCreditCardDto;
import com.treasure.service.mapper.TBankConfigMapper;
import com.treasure.service.mapper.TCreditCardMapper;
import com.treasure.service.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 银行或渠道商配置信息
 *
 * @author <a href="mailto:panxinwen@yingu.com">Pan Xinwen</a>
 * @version 1.0 2018-10-30 11:06
 */
@Service
public class BankConfigServiceImpl extends BaseService implements BankConfigService {

    @Autowired
    private TBankConfigMapper tBankConfigMapper;

    @Autowired
    private TCreditCardMapper tCreditCardMapper;

    @Override
    public PageResult<List<TBankConfigDto>> getBankConfigList(Integer pageSize, Integer pageNum,Integer status,Integer recommend) {

        Page page = PageHelper.startPage(pageNum,pageSize);

        TBankConfigExample example = new TBankConfigExample();
        TBankConfigExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(status)){
            criteria.andStatusEqualTo(status);
        }
        if (!StringUtil.isEmpty(recommend)){
            criteria.andIsRecommendEqualTo(recommend);
        }

        example.setOrderByClause("create_time desc");
        List<TBankConfig> list = tBankConfigMapper.selectByExample(example);

        List<TBankConfigDto> result = new ArrayList<>();

        if (list != null){

            list.forEach(tBankConfig -> {

                result.add(BeanUtil.copy(tBankConfig,TBankConfigDto::new));

            });

        }

        return new PageResult(page,page);
    }

    @Override
    public Integer updateBankConfig(TBankConfig tBankConfig) {
        if (StringUtil.isEmpty(tBankConfig.getId())){
            return tBankConfigMapper.insert(tBankConfig);
        }else {
            return tBankConfigMapper.updateByPrimaryKeySelective(tBankConfig);
        }
    }

    @Override
    public PageResult<List<TCreditCardDto>> getCreditList(Integer pageSize, Integer pageNum, Integer status) {

        Page page = PageHelper.startPage(pageNum,pageSize);

        TCreditCardExample tCreditCardExample = new TCreditCardExample();
        tCreditCardExample.createCriteria().andStatusEqualTo(status);
        tCreditCardExample.setOrderByClause("sort_num");
        List<TCreditCard> list = tCreditCardMapper.selectByExample(tCreditCardExample);
        List<TCreditCardDto> result = new ArrayList<>();

        if (list != null){

            list.forEach(tCreditCard -> {

                result.add(BeanUtil.copy(tCreditCard,TCreditCardDto::new));

            });

        }
        return new PageResult(page,page);
    }
}
